package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpSellerStoreMediacenter;
import com.erp.gather.domain.bo.ErpSellerStoreMediacenterBo;
import com.erp.gather.domain.vo.ErpSellerStoreMediacenterVo;
import com.erp.gather.mapper.ErpSellerStoreMediacenterMapper;
import com.erp.gather.service.IErpSellerStoreMediacenterService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * ERP媒体中心Service业务层处理
 *
 * @author erp
 * @date 2023-05-09
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreMediacenterServiceImpl implements IErpSellerStoreMediacenterService {

    private final ErpSellerStoreMediacenterMapper baseMapper;

    /**
     * 查询ERP媒体中心
     */
    @Override
    public ErpSellerStoreMediacenterVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询ERP媒体中心列表
     */
    @Override
    public TableDataInfo<ErpSellerStoreMediacenterVo> queryPageList(ErpSellerStoreMediacenterBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStoreMediacenter> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStoreMediacenterVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询ERP媒体中心列表
     */
    @Override
    public List<ErpSellerStoreMediacenterVo> queryList(ErpSellerStoreMediacenterBo bo) {
        LambdaQueryWrapper<ErpSellerStoreMediacenter> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStoreMediacenter> buildQueryWrapper(ErpSellerStoreMediacenterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerStoreMediacenter> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAeMediacenterId() != null, ErpSellerStoreMediacenter::getAeMediacenterId, bo.getAeMediacenterId());
        lqw.eq(bo.getSellerId() != null, ErpSellerStoreMediacenter::getSellerId, bo.getSellerId());
        lqw.eq(bo.getTransferStatus() != null, ErpSellerStoreMediacenter::getTransferStatus, bo.getTransferStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getBizId()), ErpSellerStoreMediacenter::getBizId, bo.getBizId());
        lqw.like(StringUtils.isNotBlank(bo.getStoreName()), ErpSellerStoreMediacenter::getStoreName, bo.getStoreName());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ErpSellerStoreMediacenter::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getFileFormat()), ErpSellerStoreMediacenter::getFileFormat, bo.getFileFormat());
        lqw.eq(bo.getSize() != null, ErpSellerStoreMediacenter::getSize, bo.getSize());
        lqw.eq(StringUtils.isNotBlank(bo.getCoverUrl()), ErpSellerStoreMediacenter::getCoverUrl, bo.getCoverUrl());
        lqw.eq(bo.getDuration() != null, ErpSellerStoreMediacenter::getDuration, bo.getDuration());
        lqw.eq(StringUtils.isNotBlank(bo.getRatio()), ErpSellerStoreMediacenter::getRatio, bo.getRatio());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), ErpSellerStoreMediacenter::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getAuditStatus()), ErpSellerStoreMediacenter::getAuditStatus, bo.getAuditStatus());
        lqw.eq(bo.getType() != null, ErpSellerStoreMediacenter::getType, bo.getType());
        lqw.eq(bo.getAliUrl() != null, ErpSellerStoreMediacenter::getAliUrl, bo.getAliUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerStoreMediacenter::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增ERP媒体中心
     */
    @Override
    public Boolean insertByBo(ErpSellerStoreMediacenterBo bo) {
        ErpSellerStoreMediacenter add = BeanUtil.toBean(bo, ErpSellerStoreMediacenter.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改ERP媒体中心
     */
    @Override
    public Boolean updateByBo(ErpSellerStoreMediacenterBo bo) {
        ErpSellerStoreMediacenter update = BeanUtil.toBean(bo, ErpSellerStoreMediacenter.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStoreMediacenter entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除ERP媒体中心
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
